/**
 * @param { object } payload
 * @param { string } payload.url
 * @param { 'GET' | 'POST' | 'PUT' | 'DELETE' } [payload.method]
 * @param { string } [payload.baseURL]
 * @param { object } [payload.params]
 * @param { object } [payload.data]
 * @param { object } [payload.header]
 */
import {getStorageSync} from './pubic.js'
const request = (payload) => {
	return new Promise((resolve, reject) => {
	if (payload.params) payload.data = payload.params
	if (payload.baseURL) payload.url = payload.baseURL + payload.url
    const satoken  = getStorageSync('satoken') || '';
	if(satoken){
		if(!payload.header){
			payload.header = {}
		}
		payload.header.satoken = satoken
	}
	const options = {
		...payload,
		success: res => {
			if (res.statusCode - 200 === 0) {
				if(res.data.code === 401){
					uni.reLaunch({
						url:'/subpackage/login/bindNo'
					})
				}
				resolve(res.data)
				return;
			}
			reject(res)
		},
		fail: err => reject
	}
	uni.request(options)
})
}
export default request